/* Source code from http://lazyfoo.not/SDL_tutorials/lesson13/index.php */
#ifndef __TIMER
#define __TIMER

#include "libraries.h"
using namespace std;

class Timer
{
    private:
        /* The clock time when the timer started */
        int startTicks;

        /* The ticks stored when the timer was paused */
        int pausedTicks;

        /* Timer status */
        bool paused;
        bool started;

    public:
        /* Initializes variables */
        Timer()
        {
            startTicks  = 0;
            pausedTicks = 0;
            paused  = false;
            started = false;
        }

        /* Reset or start the clock */
        void start()
        {
            started    = true;
            paused     = false;
            startTicks = SDL_GetTicks();
        }

        /* Stops the timer */
        void stop()
        {
            started = false;
            paused  = false;
        }

        /* Pauses the timer */
        void pause()
        {
            /* If started and not paused */
            if(( started == true ) && ( paused == false ) )
            {
                paused = true;
                pausedTicks = SDL_GetTicks() - startTicks;
            }
        }

        /* Unpauses the timer */
        void unpause()
        {
            if( paused == true )
            {
                paused = false;
                startTicks = SDL_GetTicks() - pausedTicks;
                pausedTicks = 0;
            }
        }

        /* Gets the timer's time */
        int get_ticks()
        {
            /* If not stopped */
            if( started == true )
            {
                if( paused == true )
                {
                    /* Timer is paused, so return ticks when it was paused */
                    return pausedTicks;
                }
                else
                {
                    return SDL_GetTicks() - startTicks;
                }
            }
            /* If not running */
            return 0;
        }

        /* Checks the status of the timer */
        bool is_started(){ return started; }

        bool is_paused() { return paused;  }
};

#endif
